package com.robot.tools.controller;

import com.robot.tools.service.SmsService;
import entity.Result;
import entity.Sms;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author wuyunbin
 */
@RestController
@CrossOrigin
@RequestMapping("/sms")
public class SmsController {
    @Autowired
    private SmsService smsService;

    /**
     * 发送短信
     * @param sms
     * @return
     */
    @RequestMapping(value = "",method = RequestMethod.POST)
    public Result send(@RequestBody Sms sms){
        if(!smsService.isCorrectScene(sms.getFlag())){
            return new Result(StatusCode.ERROR_FLAG_INVALID);
        }
        if (!smsService.send(sms)) {
            return new Result(StatusCode.ERROR_SEND_TOO_MANY_SMS);
        }
        return new Result(StatusCode.SUCCESS_SEND);
    }

    /**
     * 检查验证码是否过期
     * @param sms
     * @return
     */
    @RequestMapping(value = "/isCheckCodeExpire",method = RequestMethod.POST)
    public boolean isCheckCodeExpire(@RequestBody Sms sms){
       return smsService.isCheckCodeExpire(sms);
    }

    /**
     * 检验验证码是否正确
     * @param sms
     * @param checkCode
     * @return
     */
    @RequestMapping(value = "/isCorrectCheckCode/{checkCode}",method = RequestMethod.POST)
    public boolean isCorrectCheckCode(@RequestBody Sms sms, @PathVariable String checkCode){
        return smsService.isCorrectCheckCode(sms,checkCode);
    }

    /**
     * 删除验证码
     * @param sms
     */
    @RequestMapping(value = "/deleteCheckCode",method = RequestMethod.POST)
    public void deleteCheckCode(@RequestBody Sms sms){
        smsService.deleteCheckCode(sms);
    }


}
